package com.zebraphp.devtool.httpUtils.NetOkHttp.callback;


import com.zebraphp.devtool.httpUtils.NetOkHttp.IOkHttp;
import com.zebraphp.devtool.httpUtils.NetOkHttp.responseHandler.BaseHandler;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 继承OkHttp3的Callback回调函数
 */
public class ICallback implements Callback {

    private BaseHandler mResponseHandler;

    public ICallback(BaseHandler responseHandler) {
        mResponseHandler = responseHandler;
    }

    @Override
    public void onFailure(Call call, final IOException e) {
        IOkHttp.mHandlerQueue.post(new Runnable() {
            @Override
            public void run() {
                mResponseHandler.onError(0, e.toString());
            }
        });
    }

    @Override
    public void onResponse(Call call, final Response response) {
        if(response.isSuccessful()) {
            mResponseHandler.onOk(response);

        } else {
            IOkHttp.mHandlerQueue.post(new Runnable() {
                @Override
                public void run() {
                    mResponseHandler.onError(response.code(), "fail status=" + response.code());
                }
            });
        }
    }
}
